package xin.tomdonkey.chat.udp;

import io.netty.channel.unix.DatagramSocketAddress;
import xin.tomdonkey.chat.udp.protocal.SP;

import java.io.IOException;
import java.net.*;

/**
 * @author guolinyuan
 */
public class Client
{
    public static void main(String[] args) throws IOException, InterruptedException
    {
        DatagramSocket socket = new DatagramSocket();
        SocketAddress serverAddress = new InetSocketAddress("127.0.0.1",5000);

        while (true)
        {
            byte[] bs = SP.text("现在是" + System.currentTimeMillis()).toBytes();
            DatagramPacket sendPacket = new DatagramPacket(bs, bs.length, serverAddress);

            socket.send(sendPacket);

            byte[] receiveBytes = new byte[SP.UDP_MAX_SIZE];
            DatagramPacket receivePacket = new DatagramPacket(receiveBytes, receiveBytes.length);

            socket.receive(receivePacket);
            System.out.println(SP.parseSP(receivePacket.getData()).toString());

            Thread.sleep(2000);
        }
    }
}
